package com.platform.server.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.platform.server.dao.TopicDao;
import com.platform.server.service.TopicService;

@Controller
public class TopicController {
	
	@Autowired
	private TopicService topicService;
	
	@Autowired
	private TopicDao topicDao;
	
	/**
	 * 话题列表
	 */
	@RequestMapping("/selectTopicList")
	public Object selectTopicList(HttpServletRequest request,String id,String startTime,String endTime,String is_ban,String topic_title){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, Object>> lists = topicService.selectTopicList(startTime, endTime, is_ban, topic_title);
		//查看话题参与人数
		for(int i = 0;i < lists.size();i++){
			Integer join_num = topicDao.selectTopicJoinnum((Integer)lists.get(i).get("id"));
			lists.get(i).put("join_num", join_num);
			lists.get(i).put("create_time", sdf.format(lists.get(i).get("create_time")));
		}
		request.setAttribute("lists",lists);
		request.setAttribute("nums",lists.size());
		return "/ui/topic/topic_list.jsp";
	}
	
	/**
	 * 删除话题
	 */
	@RequestMapping("/deleteTopic")
	public Object deleteTopic(String idd){
		int flag  = topicService.deleteTopic(idd);
		return "/selectTopicList";
	}
	
	/**
	 * 修改话题状态
	 */
	@RequestMapping("/updateTopicStatus")
	public Object updateTopicStatus(Integer idd,String isban){
		int flag  = topicService.updateTopicStatus(idd,isban);
		return "/selectTopicList";
	}
	
	/**
	 * 添加话题
	 */
	@RequestMapping("/insertTopic")
	public void insertTopic(String topic_title,String topic_image,String is_ban,HttpServletResponse response) throws Exception{
		int flag  = topicService.insertTopic(topic_title, topic_image, is_ban);
		JSONObject json = new JSONObject();
		json.put("flag", flag);
		response.getOutputStream().write(json.toString().getBytes("utf-8"));
	}
	
	/**
	 * 话题动态列表
	 */
	@RequestMapping("/selectTopicDynamic")
	public Object selectTopicDynamic(HttpServletRequest request,String topic_id,String startTime,String endTime,String username){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, Object>> lists = topicService.selectTopicDynamic(topic_id, startTime, endTime, username);
		//查看话题参与人数
		for(int i = 0;i < lists.size();i++){
			lists.get(i).put("create_time", sdf.format(lists.get(i).get("create_time")));
		}
		request.setAttribute("lists",lists);
		request.setAttribute("nums",lists.size());
		request.setAttribute("topic_id",topic_id);
		return "/ui/topic/topic_dynamic_list.jsp";
	}
	
	/**
	 * 删除话题动态
	 */
	@RequestMapping("/deleteTopicDynamic")
	public Object deleteTopicDynamic(String id,String topic_id){
		int flag  = topicService.deleteTopicDynamic(id);
		return "/selectTopicDynamic";
	}
	
	/**
	 * 查看话题动态的点赞信息
	 */
	@RequestMapping("/selectTopicLike")
	public Object selectTopicLike(String topic_dynamic_id,HttpServletRequest request){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, Object>> lists = topicService.selectTopicLike(topic_dynamic_id);
		for(int i = 0;i < lists.size();i++){
			lists.get(i).put("create_time", sdf.format(lists.get(i).get("create_time")));
		}
		request.setAttribute("lists",lists);
		request.setAttribute("nums",lists.size());
		return "/ui/topic/dynamic_like.jsp";
	}
	
	/**
	 * 查看话题动态的评论信息
	 */
	@RequestMapping("/selectTopicComment")
	public Object selectTopicComment(String topic_dynamic_id,HttpServletRequest request){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, Object>> lists = topicService.selectTopicComment(topic_dynamic_id);
		for(int i = 0;i < lists.size();i++){
			lists.get(i).put("create_time", sdf.format(lists.get(i).get("create_time")));
		}
		request.setAttribute("lists",lists);
		request.setAttribute("nums",lists.size());
		return "/ui/topic/dynamic_comment.jsp";
	}
	
	/**
	 * 删除评论消息
	 */
	@RequestMapping("/deleteCommentMessage")
	public Object deleteCommentMessage(String id,String dynamic_id){
		int flag  = topicService.deleteCommentMessage(Integer.parseInt(id));
		return "/selectTopicComment?topic_dynamic_id=" + dynamic_id;
	}
}
